package com.snail.snailvr.views;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.snail.snailvr.R;
import com.snail.snailvr.utils.DialogUtils;
import com.snail.snailvr.views.adapter.FeedbackPreviewAdapter;

import java.io.Serializable;
import java.util.List;
import butterknife.Bind;

public class ImagePreviewActivity extends BaseToolbarActivity {

    @Bind(R.id.viewpager)
    ViewPager viewpager;

    @Bind(R.id.toolbar)
    Toolbar toolbar;

    private FeedbackPreviewAdapter adapter;

    private int INDEX;

    public static final String URI_KEY = "uri_key";

    public static Intent getIntent(Context context, Serializable serial){
        Intent intent = new Intent(context, ImagePreviewActivity.class);
        intent.putExtra(URI_KEY, serial);
        return intent;
    }

    @Override
    protected void handleIntent() {
        adapter = new FeedbackPreviewAdapter(this, (List<Uri>) getIntent().getSerializableExtra(URI_KEY));
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra(URI_KEY, (Serializable)adapter.getImages());
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void initView() {
        String title = "1/" + adapter.getImages().size();
        setupToolbar(toolbar, R.color.feedback_toolbar, R.color.toolbar_title_color, title, R.menu.menu_feedback_delete);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra(URI_KEY, (Serializable)adapter.getImages());
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        viewpager.setAdapter(adapter);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                INDEX = position;// 首先保存下来

                position++;
                toolbar.setTitle(position + "/" + adapter.getImages().size());
            }

            @Override
            public void onPageSelected(int position) {}

            @Override
            public void onPageScrollStateChanged(int state) {}
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_image_preview;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected boolean onToolbarMenuItemClick(MenuItem item) {
        if(item.getItemId() == R.id.action_clear){
            DialogUtils.showBasicDialog(this, getResources().getString(R.string.delete), getResources().getString(R.string.feedback_delete),
                    getResources().getString(R.string.cancel), getResources().getString(R.string.delete), true, new View.OnClickListener(){
                        @Override
                        public void onClick(View view) {
                            if (adapter.getImages().size() == 1){
                                // 1张
                                adapter.removeItem(INDEX);

                                Intent intent = new Intent();
                                intent.putExtra(URI_KEY, (Serializable) adapter.getImages());
                                setResult(RESULT_OK, intent);
                                finish();
                            }
                            else{
                                // 多张
                                if (INDEX == 0){
                                    adapter.removeItem(INDEX);
                                    toolbar.setTitle("1/" + adapter.getImages().size());
                                }
                                else if (INDEX == (adapter.getImages().size()-1)){
                                    adapter.removeItem(INDEX);
                                    toolbar.setTitle(adapter.getImages().size() + "/" + adapter.getImages().size());
                                    viewpager.setCurrentItem(adapter.getImages().size()-1, true);
                                }
                                else{
                                    adapter.removeItem(INDEX);
                                    viewpager.setCurrentItem(INDEX, true);
                                    toolbar.setTitle((INDEX+1) + "/" + adapter.getImages().size());
                                }
                            }
                        }
                    });

            return true;
        }

        return false;
    }
}